/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.rest.resource;

import cz.muni.fi.pa165.bookingmanager.adapters.CustomerAdapter;
import cz.muni.fi.pa165.bookingmanager.dto.CustomerDTO;
import cz.muni.fi.pa165.bookingmanager.exceptions.IllegalOrphanException;
import cz.muni.fi.pa165.bookingmanager.exceptions.NonexistentEntityException;
import cz.muni.fi.pa165.bookingmanager.exceptions.PreexistingEntityException;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author Jiří Holuša
 */
@Path("/customer")
@Stateless
public class CustomerRESTFacade {

    @EJB
    private CustomerAdapter customerAdapter;
    
    public CustomerRESTFacade() {
    }

    @POST
    @Consumes({"application/xml", "application/json"})
    public void create(CustomerDTO customer) throws IllegalOrphanException, PreexistingEntityException{
        customerAdapter.createCustomer(customer);
    }

    @PUT    
    @Consumes({"application/xml", "application/json"})
    public void edit(CustomerDTO customer) throws NonexistentEntityException, IllegalOrphanException, PreexistingEntityException, Exception {
        customerAdapter.updateCustomer(customer);
    }
  

    @POST
    @Path("delete")
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public void remove(CustomerDTO customer) throws NonexistentEntityException, IllegalOrphanException {
             customerAdapter.deleteCustomer(customer);        
    }

    @GET
    @Path("{id}")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public CustomerDTO get(@PathParam("id") Long id) {
        return customerAdapter.getCustomerById(id);
    }   

    @GET
    @Path("allByName-{name}")    
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public List<CustomerDTO> findAllByName(@PathParam("name") String name) {
        List<CustomerDTO> customers = customerAdapter.findCustomersByName(name);
        return customers;
    }
    
    @GET 
    @Path("all")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public List<CustomerDTO> findAll() {        
        List<CustomerDTO> customers = customerAdapter.findAllCustomers();
        return customers;
    }
    
}
